package com.jbp.utils.qiniuyun;


import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.jbp.utils.HttpClient;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.http.client.methods.HttpPost;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 七牛云工具类
 * 提供七牛云存储和内容审核功能
 * 包括文件上传Token获取和文本内容安全检测
 *
 * @author system
 */
public class Qiniuyun {

    /**
     * 七牛云访问密钥
     * 用于身份验证和API调用
     */
//    private static String ACCESS_KEY = "hC8666oZcnDUaWF67ESvhjMy2hLAAbCBZw4SwhVW";
    private static String ACCESS_KEY = "B2fKyJkh8qh0MK3jHBA2f6J4Q_y6Osz4UosGeqIP";

    /**
     * 七牛云私钥
     * 用于生成签名和Token
     */
//    private static String SECRET_KEY = "e8HLRmjMQHE3LhEIz6kGVADbW_oqyRgyOpRpcfbP";
    private static String SECRET_KEY = "SLB8-JFGdVzhuSA4lQ6g9EDXHufLRtlgiBuy23WM";

//    private static  Mac mac = new Mac(ACCESS_KEY,SECRET_KEY);
//    private static PutPolicy putPolicy;
//    private static PutExtra extra = new PutExtra();
/*
    public static String upInput(String key, InputStream inputStream) throws Exception{
        String bucketName = "yzb2:"+key;
        putPolicy = new PutPolicy(bucketName);
        String uptoken = putPolicy.token(mac);
        PutRet ret = IoApi.Put(uptoken,key,inputStream,extra);
        System.out.println(ret.getResponse());
        return ret.getKey();
    }*/
    /**
     * 获取七牛云上传Token
     * 生成用于文件上传的授权Token
     *
     * @return 上传Token字符串
     * @throws Exception 生成Token时可能抛出的异常
     */
    public static String getToken() throws Exception{
        /*String key = UUID.randomUUID().toString();
        String bucketName = "yzb2";
        putPolicy = new PutPolicy(bucketName);
        String uptoken = putPolicy.token(mac);
        return uptoken;*/
//        String bucketName = "xingxing152";
        String bucketName = "tangdou001";
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String token = auth.uploadToken(bucketName);
        return token;
    }

    /**
     * 文本内容安全检测
     * 使用七牛云AI内容审核服务检测文本是否包含违规内容
     *
     * @param content 需要检测的文本内容
     * @return true表示内容安全通过，false表示内容存在违规
     */
    public static boolean check(String content) {
        Map<String, Object> data = new HashMap<>();
        data.put("text", content);
        Map<String, Object> params = new HashMap<>();
        params.put("scenes", new String[]{"antispam"});
        Map<String, Object> m = new HashMap<>();
        m.put("data", data);
        m.put("params", params);
        String j = JSON.toJSONString(m);
        String sign = getSign(j);

        String url = "http://ai.qiniuapi.com/v3/text/censor";
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type","application/json");
        header.put("Authorization","Qiniu "+sign);
        String r = HttpClient.doPostJson(j, header, url);
        String suggess = JSON.parseObject(r).getJSONObject("result").getString("suggestion");

        return "pass".equals(suggess);
    }
    /**
     * 生成七牛云API签名
     * 为七牛云API请求生成授权签名
     *
     * @param body 请求体内容
     * @return 签名字符串，生成失败返回null
     */
    public static String getSign(String body) {
        try {
            String url = "http://ai.qiniuapi.com/v3/text/censor";
            String method = "POST";
            String contentType = "application/json";

            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
            String result = auth.signQiniuAuthorization(url, method, body.getBytes(), contentType);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试方法
     * 用于测试文本内容安全检测功能
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        System.out.println(check("习近p"));
    }
}
